<script setup lang="ts">
import { computed, reactive, watch } from 'vue';
import { $t } from '@/locales';
import type { LogLogin } from '@/service/api';
import DictTag from '@/components/custom/dict-tag/src/dict-tag.vue';
import { DICT_TYPE } from '@/utils/dict';

defineOptions({
  name: 'LogLoginDetailModel'
});

interface Props {
  /** the edit row data */
  rowData?: LogLogin | null;
}

const props = defineProps<Props>();

const visible = defineModel<boolean>('visible', {
  default: false
});

const title = computed(() => {
  return $t('common.detail');
});

type Model = Pick<
  LogLogin,
  | 'id'
  | 'logType'
  | 'traceId'
  | 'userId'
  | 'userType'
  | 'username'
  | 'result'
  | 'loginLocation'
  | 'userIp'
  | 'browser'
  | 'os'
  | 'createTime'
>;

const model: Model = reactive(createDefaultModel());

function createDefaultModel(): Model {
  return {
    id: 0,
    logType: 0,
    traceId: '',
    userId: 0,
    userType: 0,
    username: '',
    result: 0,
    loginLocation: '',
    userIp: '',
    browser: '',
    os: '',
    createTime: ''
  };
}

function handleInitModel() {
  Object.assign(model, createDefaultModel());

  if (props.rowData) {
    Object.assign(model, props.rowData);
  }
}

function closeDrawer() {
  visible.value = false;
}

watch(visible, () => {
  if (visible.value) {
    handleInitModel();
  }
});
</script>

<template>
  <NModal v-model:show="visible" :title="title" preset="card" class="w-200">
    <NScrollbar class="max-h-200 pr-20px">
      <NDescriptions :column="1" label-placement="left" bordered>
        <NDescriptionsItem :label="$t('page.manage.log.login.id')">
          {{ model.id }}
        </NDescriptionsItem>
        <NDescriptionsItem :label="$t('page.manage.log.login.traceId')">
          {{ model.traceId }}
        </NDescriptionsItem>
        <NDescriptionsItem :label="$t('page.manage.log.login.logType')">
          <DictTag :type="DICT_TYPE.SYSTEM_LOGIN_TYPE" :value="model.logType" />
        </NDescriptionsItem>
        <NDescriptionsItem :label="$t('page.manage.log.login.traceId')">
          {{ model.username }}
        </NDescriptionsItem>
        <NDescriptionsItem :label="$t('page.manage.log.login.result')">
          <DictTag :type="DICT_TYPE.SYSTEM_LOGIN_RESULT" :value="model.result" />
        </NDescriptionsItem>
        <NDescriptionsItem :label="$t('page.manage.log.login.userIp')">
          {{ model.userIp }}
        </NDescriptionsItem>
        <NDescriptionsItem :label="$t('page.manage.log.login.browser')">
          {{ model.browser }}
        </NDescriptionsItem>
        <NDescriptionsItem :label="$t('page.manage.log.login.os')">
          {{ model.os }}
        </NDescriptionsItem>
        <NDescriptionsItem :label="$t('page.manage.log.login.loginLocation')">
          {{ model.loginLocation }}
        </NDescriptionsItem>
        <NDescriptionsItem :label="$t('page.manage.log.login.createTime')">
          {{ model.createTime }}
        </NDescriptionsItem>
      </NDescriptions>
    </NScrollbar>
    <template #footer>
      <NSpace justify="end" :size="16">
        <NButton @click="closeDrawer">{{ $t('common.close') }}</NButton>
      </NSpace>
    </template>
  </NModal>
</template>

<style scoped></style>
